package windbell.domain.risk.impl;

import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;
import windbell.domain.risk.RiskCategory;
import windbell.domain.risk.RiskCommonIssue;
import windbell.domain.risk.RiskSubCategory;
import windbell.domain.risk.impl.BaseHasSimplePyImpl;
import windbell.domain.risk.impl.RiskCategoryImpl;
import windbell.domain.risk.impl.RiskCommonIssueImpl;
import windbell.domain.risk.proxy.RiskSubCategoryProxy;

@Entity
@DiscriminatorValue("RISKSUBCATEGORY_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "risksubcategory")
public class RiskSubCategoryImpl extends BaseHasSimplePyImpl implements RiskSubCategory {
  public RiskSubCategoryImpl() {
    super();
  }
  
  public RiskSubCategoryImpl(final RiskCategory category) {
    super();
    this.category = category;
  }
  
  @JoinColumn(name = "category_id")
  @ManyToOne(targetEntity = RiskCategoryImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RiskCategory category;
  
  public RiskCategory getCategory() {
    return this.category;
  }
  
  @OneToMany(targetEntity = RiskCommonIssueImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "subCategory")
  private Collection<RiskCommonIssue> commonIssues = new java.util.ArrayList<windbell.domain.risk.RiskCommonIssue>();;
  
  public Collection<RiskCommonIssue> getCommonIssues() {
    return this.commonIssues;
  }
  
  public RiskCommonIssue createAndAddtoCommonIssues() {
    getCommonIssues().size();
    windbell.domain.risk.RiskCommonIssue riskCommonIssue = new windbell.domain.risk.impl.RiskCommonIssueImpl(this);
    getObjectFactory().create(riskCommonIssue);
    getCommonIssues().add(riskCommonIssue);
    return riskCommonIssue;
    
  }
  
  public RiskCommonIssue createAndAddtoCommonIssues(final Procedure1<RiskCommonIssue> updater) {
    getCommonIssues().size();
    windbell.domain.risk.RiskCommonIssue riskCommonIssue = new windbell.domain.risk.impl.RiskCommonIssueImpl(this);
    getObjectFactory().create(riskCommonIssue);
    getCommonIssues().add(riskCommonIssue);
    return riskCommonIssue;
    
  }
  
  public RiskSubCategory removeAllCommonIssues() {
    for(RiskCommonIssue obj : this.commonIssues){
    	getObjectFactory().delete(obj);
    }
    this.commonIssues.clear();
    return this;	
    
  }
  
  public RiskSubCategory removeFromCommonIssues(final RiskCommonIssue riskCommonIssue) {
    this.commonIssues.remove(riskCommonIssue);
    getObjectFactory().delete(riskCommonIssue);
    return this;
    
  }
  
  public static RiskSubCategory create(final RiskCategory category, final IObjectFactory objectFactory) {
    windbell.domain.risk.RiskSubCategory riskSubCategory = new windbell.domain.risk.impl.RiskSubCategoryImpl(
    	category
    );
    objectFactory.create(riskSubCategory);
    return riskSubCategory;			
    
  }
  
  public static RiskSubCategory createRiskSubCategoryImpl(final RiskCategory category, final IObjectFactory objectFactory, final Procedure1<RiskSubCategory> init) {
    windbell.domain.risk.RiskSubCategory riskSubCategory = new windbell.domain.risk.impl.RiskSubCategoryImpl(
    	category
    );
    objectFactory.create(riskSubCategory);
    init.apply(riskSubCategory);
    return riskSubCategory;			
    
  }
  
  public RiskSubCategoryProxy toProxy() {
    windbell.domain.risk.proxy.RiskSubCategoryProxy proxy = new windbell.domain.risk.proxy.RiskSubCategoryProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
